package com.example.demo.controller;

import com.example.demo.service.User_PaymentService;
import com.example.demo.util.AlipayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class AlipayController {
    @Autowired
    User_PaymentService service;

    private AlipayUtil alipayUtil;

    @Autowired
    public  void setAlipayUtil(AlipayUtil alipayUtil){
        this.alipayUtil = alipayUtil;
    }

    @GetMapping("/pay")
    public ModelAndView indexPay(@RequestParam String id){
        ModelAndView mav = new ModelAndView("indexPay");

        String[] array = new String[0];
        array = id.split(",");
        String idNo = array[0];
        String value = array[1];
        String payment_id = array[2];

        String[] array2 = new String[0];
        array2 = value.split("=");
        value = array2[1];

        mav.addObject("idNo", idNo);
        mav.addObject("value", value);
        mav.addObject("payment_id", payment_id);
        return mav;
    }

    @PostMapping("/create")
    public String create(String id, String price, String title, Model model){
        String pay = alipayUtil.pay(id, price, title);
        model.addAttribute("form",pay);
        return "pay";
    }

    @GetMapping("/return")
    public String returnNotice(String out_trade_no,Model model){
        String query = alipayUtil.query(out_trade_no);
        model.addAttribute("query",query);
        int i = service.jiaofei(Integer.parseInt(out_trade_no));
        return "query";
    }

    @PostMapping("/notify")
    public void notifyUrl(String trade_no,String total_amount,String trade_status){
        System.out.println("支付宝订单编号:" + trade_no + ",订单金额:" + total_amount + ",订单状态:" + trade_status);
    }
}
